package com.common.repostitory.impl;

import com.common.tool.Page;
import com.ssh.entity.Repair;
import com.common.repostitory.RepairRepository;
import com.ssh.requestPackage.RepairRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 文 件 名 称：RepairRepositoryImpl.
 * 文 件 说 明：
 * 创   建   人：zpen.
 * 创 建 时 间：2018/3/1 16:05.
 */
@Repository
@Transactional
public class RepairRepositoryImpl extends BaseRepostitoryImpl<Repair,Long> implements RepairRepository {

    @Override
    public List<Map<String, Object>> queryForPage(RepairRequest repairRequest,Page page) {
        StringBuffer querySql =new StringBuffer();
        List queryParam = new ArrayList();
        List<Map<String, Object>> result = new ArrayList<>();
        querySql.append("select rep.*,com.name con_name,com.sfz_no con_sfz,com.username,com.phone con_phone,mec.name mec_name,mec.sfz_no mec_sfz,mec.phone mec_phone ");
        querySql.append("from zpen.repair rep left join zpen.consumer com  on rep.con_id = com.id left join zpen.mechanic mec on rep.complete_id = mec.id ");
        querySql.append("where 1=1 ");
        try{ if(!StringUtils.isBlank(repairRequest.getMecName())){
            querySql.append("and mec.name like '%"+repairRequest.getMecName()+"%' ");
        }
            if(!StringUtils.isBlank(repairRequest.getMecSfzNo())){
                querySql.append("and mec.sfz_no = ? ");
                queryParam.add(repairRequest.getMecSfzNo());
            }
            if(!StringUtils.isBlank(repairRequest.getConName())){
                querySql.append("and mec.phone = ? ");
                queryParam.add(repairRequest.getMecPhone());
            }
            if(!StringUtils.isBlank(repairRequest.getConName())){
                querySql.append("and mec.name like '%"+repairRequest.getConName()+"%' ");
            }
            if(!StringUtils.isBlank(repairRequest.getConSfzNo())){
                querySql.append("and com.sfz_no = ? ");
                queryParam.add(repairRequest.getConSfzNo());
            }
            if(!StringUtils.isBlank(repairRequest.getConPhone())){
                querySql.append("and com.phone = ? ");
                queryParam.add(repairRequest.getConPhone());
            }
            if(!StringUtils.isBlank(repairRequest.getUsername())){
                querySql.append("and com.username like '%"+repairRequest.getUsername()+"%' ");
            }
            if(!StringUtils.isBlank(repairRequest.getOrderNo())){
                querySql.append("and rep.order_no = ? ");
                queryParam.add(repairRequest.getOrderNo());
            }
            if(!StringUtils.isBlank( repairRequest.getCreateTimeBeg())){
                querySql.append("and substring(rep.create_time,1,10) >= ? ");
                queryParam.add(repairRequest.getCreateTimeBeg());
            }
            if(!StringUtils.isBlank( repairRequest.getCreateTimeEnd())){
                querySql.append("and substring(rep.create_time,1,10) <= ? ");
                queryParam.add(repairRequest.getCreateTimeEnd());
            }
            if(!StringUtils.isBlank( repairRequest.getExpectRepairTimeBeg())){
                querySql.append("and substring(rep.expect_repair_time,1,10) >= ? ");
                queryParam.add(repairRequest.getExpectRepairTimeBeg());
            }
            if(!StringUtils.isBlank( repairRequest.getExpectRepairTimeEnd())){
                querySql.append("and substring(rep.expect_repair_time,1,10) <= ? ");
                queryParam.add(repairRequest.getExpectRepairTimeEnd());
            }
            if(!StringUtils.isBlank(repairRequest.getRepairType())){
                querySql.append("and rep.repair_type = ? ");
                queryParam.add(repairRequest.getRepairType());
            }
            if(!StringUtils.isBlank(repairRequest.getDeviceType())){
                querySql.append("and rep.device_type = ? ");
                queryParam.add(repairRequest.getDeviceType());
            }
            if(!StringUtils.isBlank(page.getSort())){
                querySql.append("order by ? ?");
                queryParam.add(page.getSort());
                queryParam.add(page.getOrder());
            }
            querySql.append(" limit ? ,?");
            queryParam.add(Integer.valueOf(page.getOffset()));
            queryParam.add(Integer.valueOf(page.getLimit()));


             result = findSql(querySql.toString(),queryParam.toArray());
        }catch (Exception e){
            e.printStackTrace();
        }

        return result;
    }

    @Override
    public Integer queryForAll(RepairRequest repairRequest,Page page) {
        StringBuffer querySql =new StringBuffer();
        List queryParam = new ArrayList();
        querySql.append("select count(*) counts ");
        querySql.append("from zpen.repair rep left join zpen.consumer com  on rep.con_id = com.id left join zpen.mechanic mec on rep.complete_id = mec.id ");
        querySql.append("where 1=1 ");
        Integer count = 0;
        try{
            if(!StringUtils.isBlank(repairRequest.getMecName())){
            querySql.append("and mec.name like '%"+repairRequest.getMecName()+"%' ");
            }
            if(!StringUtils.isBlank(repairRequest.getMecSfzNo())){
                querySql.append("and mec.sfz_no = ? ");
                queryParam.add(repairRequest.getMecSfzNo());
            }
            if(!StringUtils.isBlank(repairRequest.getConName())){
                querySql.append("and mec.phone = ? ");
                queryParam.add(repairRequest.getMecPhone());
            }
            if(!StringUtils.isBlank(repairRequest.getConName())){
                querySql.append("and mec.name like '%"+repairRequest.getConName()+"%' ");
            }
            if(!StringUtils.isBlank(repairRequest.getConSfzNo())){
                querySql.append("and com.sfz_no = ? ");
                queryParam.add(repairRequest.getConSfzNo());
            }
            if(!StringUtils.isBlank(repairRequest.getConPhone())){
                querySql.append("and com.phone = ? ");
                queryParam.add(repairRequest.getConPhone());
            }
            if(!StringUtils.isBlank(repairRequest.getUsername())){
                querySql.append("and com.username like '%"+repairRequest.getUsername()+"%' ");
            }
            if(!StringUtils.isBlank(repairRequest.getOrderNo())){
                querySql.append("and rep.order_no = ? ");
                queryParam.add(repairRequest.getOrderNo());
            }
            if(!StringUtils.isBlank( repairRequest.getCreateTimeBeg())){
                querySql.append("and substring(rep.create_time,1,10) >= ? ");
                queryParam.add(repairRequest.getCreateTimeBeg());
            }
            if(!StringUtils.isBlank( repairRequest.getCreateTimeEnd())){
                querySql.append("and substring(rep.create_time,1,10) <= ? ");
                queryParam.add(repairRequest.getCreateTimeEnd());
            }
            if(!StringUtils.isBlank( repairRequest.getExpectRepairTimeBeg())){
                querySql.append("and substring(rep.expect_repair_time,1,10) >= ? ");
                queryParam.add(repairRequest.getExpectRepairTimeBeg());
            }
            if(!StringUtils.isBlank( repairRequest.getExpectRepairTimeEnd())){
                querySql.append("and substring(rep.expect_repair_time,1,10) <= ? ");
                queryParam.add(repairRequest.getExpectRepairTimeEnd());
            }
            if(!StringUtils.isBlank(repairRequest.getRepairType())){
                querySql.append("and rep.repair_type = ? ");
                queryParam.add(repairRequest.getRepairType());
            }
            if(!StringUtils.isBlank(repairRequest.getDeviceType())){
                querySql.append("and rep.device_type = ? ");
                queryParam.add(repairRequest.getDeviceType());
            }
            count = queryForObject(querySql.toString(),queryParam.toArray());
        }catch (Exception e){e.printStackTrace();}

        return count;
    }
}
